//
//  ShowMsg.m
//  提示语_心之寻
//
//  Created by Burn on 2019/3/28.
//  Copyright © 2019 menksoft. All rights reserved.
//
#define UICOLOR_RGB(R, G, B, A) [UIColor colorWithRed:R / 255.0 green:G / 255.0 blue:B / 255.0 alpha:A]
#import "ShowMsg.h"

@interface ShowMsg()

@property(strong,nonatomic) UILabel *msg;
@property(copy,nonatomic) NSString *text;
@property(copy,nonatomic)NSString *headerText;
@property(assign,nonatomic) CGFloat labelH;

@end

@implementation ShowMsg


- (void)showInView:(UIView *)view msg:(NSString *)msg{
    self.text = msg;
    if (view == nil) {
        view = [UIApplication sharedApplication].keyWindow;
    }
    
    /*
     UITabBarController *tabbarController = (UITabBarController *)[UIApplication sharedApplication].keyWindow.rootViewController;
     UINavigationController *nav = [tabbarController.childViewControllers firstObject];
     CGRect statuF = [UIApplication sharedApplication].statusBarFrame;
     CGRect navF = nav.navigationBar.frame;
     */
    [self msg];
    self.frame = CGRectMake(0, 0, 30, self.labelH);
    self.backgroundColor = [UIColor whiteColor];
    self.msg.frame = CGRectMake(0, 0, 30, self.labelH);
    self.center = view.center;
    
    [self addSubview:self.msg];
    [view addSubview:self];
}


- (UILabel *)msg{
    if (!_msg) {
        NSMutableAttributedString *attriStr = [[NSMutableAttributedString alloc]initWithString:self.headerText];
        if (self.text.length > 0) {
            [attriStr addAttribute:NSForegroundColorAttributeName value:UICOLOR_RGB(82, 126, 173, 1) range:NSMakeRange(0, self.text.length + 2)];
            [attriStr addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:NSMakeRange(self.text.length + 2, attriStr.length - self.text.length - 2)];
        }else{
            [attriStr addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:NSMakeRange(0, self.text.length)];
        }
    
        _msg = [[UILabel alloc]init];
        _msg.transform = CGAffineTransformScale(_msg.transform, -1, 1);
        _msg.transform =  CGAffineTransformRotate(_msg.transform, M_PI / 2);
        _msg.font = [UIFont fontWithName:@"MenksoftQagan_mirror" size:17];
        _msg.numberOfLines = 0;
        _msg.attributedText = attriStr;
        _msg.backgroundColor = [UIColor whiteColor];
        _msg.textAlignment = NSTextAlignmentCenter;
        
//        CGSize maximumLabelSize = CGSizeMake(30, 9999);
//        CGSize expectSize = [_msg sizeThatFits:maximumLabelSize];
//        self.labelH = expectSize.height;
        
        CGSize attSize = [attriStr boundingRectWithSize:CGSizeMake(30, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading context:nil].size;
        self.labelH = attSize.height;
        
    }
    return _msg;
}


- (void)dismiss
{
    [self removeFromSuperview];
}



- (NSString *)headerText{
    if (!_headerText) {
        if (self.text.length > 0) {
            _headerText = [NSString stringWithFormat:@"\'%@\'    ",self.text];
        }else{
            _headerText = [NSString stringWithFormat:@"    "];
        }
    }
    return _headerText;
}
@end
